<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">
<head>
    <th:block th:include="include :: header('修改角色')"/>
    <th:block th:include="include :: ztree-css"/>
</head>
<body class="white-bg">
<div class="wrapper wrapper-content animated fadeInRight ibox-content">
    <form class="form-horizontal m" id="form-role-edit" th:object="${role}">
        <input id="id" name="id" th:field="*{id}" type="hidden"/>
        <div class="form-group">
            <label class="col-sm-3 control-label is-required">角色编码：</label>
            <div class="col-sm-8">
                <input class="form-control" id="code" name="code" required th:field="*{code}" type="text">
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label is-required">角色名称：</label>
            <div class="col-sm-8">
                <input class="form-control" id="name" name="name" required th:field="*{name}" type="text">
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">备注：</label>
            <div class="col-sm-8">
                <input class="form-control" id="remark" name="remark" th:field="*{remark}" type="text">
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">菜单权限：</label>
            <div class="col-sm-8">
                <label class="check-box">
                    <input type="checkbox" value="1">展开/折叠</label>
                <label class="check-box">
                    <input type="checkbox" value="2">全选/全不选</label>
                <label class="check-box">
                    <input checked type="checkbox" value="3">父子联动</label>
                <div class="ztree ztree-border" id="menuTrees"></div>
            </div>
        </div>
    </form>
</div>
<th:block th:include="include :: footer"/>
<th:block th:include="include :: ztree-js"/>
<script type="text/javascript">
    $(function () {
        var url = ctx + "framework/menu/roleMenuTreeData?id=" + $("#id").val();
        var options = {
            id: "menuTrees",
            url: url,
            check: {enable: true},
            expandLevel: 0
        };
        $.tree.init(options);
    });

    $("#form-role-edit").validate({
        onkeyup: false,
        rules: {
            name: {
                remote: {
                    url: ctx + "framework/role/checkRoleNameUnique",
                    type: "post",
                    dataType: "json",
                    data: {
                        "id": function () {
                            return $("#id").val();
                        },
                        "name": function () {
                            return $.common.trim($("#name").val());
                        }
                    },
                    dataFilter: function (data, type) {
                        return $.validate.unique(data);
                    }
                }
            },
            code: {
                remote: {
                    url: ctx + "framework/role/checkCodeUnique",
                    type: "post",
                    dataType: "json",
                    data: {
                        "id": function () {
                            return $("#id").val();
                        },
                        "code": function () {
                            return $.common.trim($("#code").val());
                        }
                    },
                    dataFilter: function (data, type) {
                        return $.validate.unique(data);
                    }
                }
            },
            roleSort: {
                digits: true
            },
        },
        messages: {
            "name": {
                remote: "角色名称已经存在"
            },
            "code": {
                remote: "角色编码已经存在"
            }
        },
        focusCleanup: true
    });

    $('input').on('ifChanged', function (obj) {
        var type = $(this).val();
        var checked = obj.currentTarget.checked;
        if (type == 1) {
            if (checked) {
                $._tree.expandAll(true);
            } else {
                $._tree.expandAll(false);
            }
        } else if (type == "2") {
            if (checked) {
                $._tree.checkAllNodes(true);
            } else {
                $._tree.checkAllNodes(false);
            }
        } else if (type == "3") {
            if (checked) {
                $._tree.setting.check.chkboxType = {"Y": "ps", "N": "ps"};
            } else {
                $._tree.setting.check.chkboxType = {"Y": "", "N": ""};
            }
        }
    })

    function edit() {
        var id = $("input[name='id']").val();
        var name = $("input[name='name']").val();
        var code = $("input[name='code']").val();
        var remark = $("input[name='remark']").val();
        var menuIds = $.tree.getCheckedNodes();
        $.ajax({
            cache: true,
            type: "POST",
            url: ctx + "framework/role/edit",
            data: {
                "id": id,
                "name": name,
                "code": code,
                "remark": remark,
                "menuIds": menuIds
            },
            async: false,
            error: function (request) {
                $.modal.alertError("系统错误");
            },
            success: function (data) {
                $.operate.successCallback(data);
            }
        });
    }

    function submitHandler() {
        if ($.validate.form()) {
            edit();
        }
    }
</script>
</body>
</html>
